package photobox.manage

import grails.converters.JSON
import photobox.BaseController
import photobox.photo.PhotoComment
import photobox.utils.ParamUtils
import photobox.utils.ResultUtil

class MngCommentController extends BaseController {
    def commentService
    def index() {}
    def list() {
        if(params.commentId){
            def comment = PhotoComment.get(params.commentId)
            if(comment.isRecommend == 0){

                comment.isRecommend =1
                comment.save()
            }
        }
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "comment", "dateCreated", "user","photo","isRecommend"], [sort: 'id', order: "desc"])

        def commentRs = commentService.list(searchParams, pageParams, sortParams)
        def dataList = []
        def totalRow = 0
        def curPage = pageParams.page
        def pageSize = pageParams.pageSize
        if (curPage <= 0) curPage = 1
        if (pageSize <= 0) pageSize = 5
        if (pageSize >=100) pageSize = 100
        if (totalRow == null) totalRow = 0
        int totalPage = (totalRow +  pageSize -1) / pageSize
        if(commentRs.rs){
            totalRow = commentRs.total
            commentRs.rs.each {
                dataList.push(
                        id:it.id,comment:it.comment,dateCreated:it.dateCreated,user:it.user?.nickName,photo:it.photo?.name,isRecommend:it.isRecommend
                )
            }
        }
        def result = [:]
        result.put("datas",dataList)
        result.put("records",totalRow)
        result.put("page",curPage)
        result.put("total",totalPage)
        render result as JSON
    }
}
